from pymongo import MongoClient
import datetime

# 连接到MongoDB
client = MongoClient('mongodb://localhost:27017/')
db = client.medical_system

# 获取admin角色
admin_role = db.roles.find_one({'name': 'admin'})

if admin_role:
    # 检查是否已存在这些权限
    permissions = admin_role.get('permissions', [])
    
    # 医嘱管理相关的权限
    medical_order_permissions = [
        'medical_order:read',
        'medical_order:create',
        'medical_order:update',
        'medical_order:delete',
        'medical_order:execute'
    ]
    
    # 筛选出尚未包含的权限
    permissions_to_add = [p for p in medical_order_permissions if p not in permissions]
    
    if permissions_to_add:
        # 添加缺失的权限
        updated_permissions = permissions + permissions_to_add
        
        # 更新数据库
        db.roles.update_one(
            {'name': 'admin'},
            {'$set': {
                'permissions': updated_permissions,
                'updatedAt': datetime.datetime.utcnow()
            }}
        )
        
        print(f"成功添加医嘱管理权限: {', '.join(permissions_to_add)}")
    else:
        print("admin角色已包含所有医嘱管理权限")
else:
    print("未找到admin角色")

# 关闭连接
client.close()

print("\n提示: 请确保重新登录系统，以使新权限生效!") 